#include "libmsg.h"

typedef struct _Libmsg_Subject_Observer
{
	Libmsg_Observer observer;
	void *data;
} Libmsg_Subject_Observer;

void libmsg_subject_setup(Libmsg_Subject *s)
{
	s->observers = eina_hash_string_superfast_new(NULL);
}

void libmsg_subject_observer_add(Libmsg_Subject *s, const char *event_name,
	Libmsg_Observer o, void *data)
{
	Libmsg_Subject_Observer *so;
	Eina_List *observers, *tmp;

	if (!o) return;
	if (!s) return;
	if (!event_name) return;

	so = calloc(1, sizeof(Libmsg_Subject_Observer));
	so->observer = o;
	so->data = data;

	observers = eina_hash_find(s->observers, event_name);
	tmp = eina_list_append(observers, so);
	if (!observers)
	{
		eina_hash_add(s->observers, event_name, tmp);
	}
}

/* TODO */
void libmsg_subject_observer_remove(Libmsg_Subject *s, const char *name, Libmsg_Observer o)
{
}


void libmsg_subject_notify(Libmsg_Subject *s, const char *event_name, void *event_data)
{
	Libmsg_Subject_Observer *so;
	Eina_List *observers;
	Eina_List *l;

	if (!event_name) return;
	observers = eina_hash_find(s->observers, event_name);
	if (!observers) return;

	EINA_LIST_FOREACH(observers, l, so)
	{
		so->observer(s, event_name, event_data, so->data);
	}
}
